import { MemberConst } from "src/consts/member_const";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';

const state: any = {};
// StateIniter.init(state, 'demo');

state.formData = [];
state.showData = {};
// vuex

/** 更新数据的 */
const mutations = {
    [MemberConst.CommitMemberSetList](state: any, data: any): void {
        console.log("Member_Set_Panel data update", data);
        state.formData = []
        for (let i = 0; i < data.formData.length; i++) {
            // if (data.formData[i].id != "ROLE_Lv1fuzhuxitong" && data.formData[i].id != "ROLE_Lv1qita") {
            //     state.formData.push(data.formData[i])
            // }
            if (data.formData[i].id == "ROLE_Lv1jichuguanli") {
                state.formData.push(data.formData[i])
            }

            if (data.formData[i].id == "ROLE_Lv1gonggongwangzhanguanli") {
                state.formData.push(data.formData[i])
            }

        }


        state.showData = data.showData
        WLLib.panel(PopupName.Member_Set_Panel);
    },
}

//异步处理
const actions = {
    [MemberConst.DispatchMemberSet](context: VuexData, data?: any): void {
        var senData = {}
        Api.AjaxPost(Config.ajaxUrl + "/authority/list.do", senData, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                if (WLStore().state.member.type == "set" || WLStore().state.member.type == "edit") {
                    Api.AjaxPost(Config.ajaxUrl + "/role/show.do", data, (showRes: any) => {
                        if (isNoEmptyObject(showRes.data)) {
                            var resData: any = {}
                            var showData: any = WLStore().state.memberShow
                            var arr = []
                            resData["formData"] = res.data;
                            if (WLStore().state.memberShow.Authority && isNoEmpty(WLStore().state.memberShow.Authority)) {
                                for (let i = 0; i < WLStore().state.memberShow.Authority.length; i++) {
                                    arr.push(WLStore().state.memberShow.Authority[i].id)
                                }
                                showData["authority_list"] = arr
                                resData["showData"] = showData;
                            } else {
                                resData["showData"] = showRes.data;
                            }
                            WLCommit(MemberConst.CommitMemberSetList, resData);
                        }
                    })
                } else {
                    var resData: any = {}
                    var showData: any = {}
                    var arr = []
                    if (isNoEmpty(WLStore().state.memberShow.Authority)) {
                        for (let i = 0; i < WLStore().state.memberShow.Authority.length; i++) {
                            arr.push(WLStore().state.memberShow.Authority[i].id)
                        }
                    }
                    showData["Authority"] = arr
                    resData["formData"] = res.data;
                    resData["showData"] = showData;

                    WLCommit(MemberConst.CommitMemberSetList, resData);
                }
            }
        })
    },
}

export default {
    state,
    mutations,
    actions
}